using System.Collections.Generic;
using System.Linq;
using ExternalDSL.EmailFormLanguage;
using Machine.Specifications;
using Sprache;

namespace ExternalDSLTests.EmailFormLanguage
{
	public class When_parsing_a_form
	{
		Establish context = () =>
		                    input =
		                    "Form [ From \"SENDER\" To \"RECIPIENT\" Subject \"SUBJECT\" Fields { Field1 \"FIELD1\" Field2 \"FIELD2\" } ] ";

		Because of = () =>
		             form = EmailFormParser.Form.Parse(input);

		It should_have_a_From_field = () =>
		                              	{
		                              		form.From.Name.ShouldEqual("From");
		                              		form.From.Label.ShouldEqual("SENDER");
		                              	};

		It should_have_a_To_field = () =>
		                            	{
		                            		form.To.Name.ShouldEqual("To");
		                            		form.To.Label.ShouldEqual("RECIPIENT");
		                            	};

		It should_have_a_Subject = () =>
		                           	{
		                           		form.Subject.Name.ShouldEqual("Subject");
		                           		form.Subject.Label.ShouldEqual("SUBJECT");
		                           	};

		It should_have_a_pair_of_Fields = () =>
		                                     	{
		                                     		form.Fields.Count().ShouldEqual(2);
		                                     		form.Fields.ElementAt(0).Name.ShouldEqual("Field1");
		                                     		form.Fields.ElementAt(0).Label.ShouldEqual("FIELD1");
		                                     		form.Fields.ElementAt(1).Name.ShouldEqual("Field2");
		                                     		form.Fields.ElementAt(1).Label.ShouldEqual("FIELD2");
		                                     	};

		static string input;
		static Form form;
	}

	public class When_parsing_the_user_fields
	{
		Establish establish = () =>
		                      input = "Fields { Field1 \"FIELD1\" Field2 \"FIELD2\" }";

		Because because = () =>
		                  fields = EmailFormParser.UserFields.Parse(input);

		It it_should_have_a_pair_of_Fields = () =>
		                                     	{
		                                     		fields.Count().ShouldEqual(2);
		                                     		fields.ElementAt(0).Name.ShouldEqual("Field1");
		                                     		fields.ElementAt(0).Label.ShouldEqual("FIELD1");
		                                     		fields.ElementAt(1).Name.ShouldEqual("Field2");
		                                     		fields.ElementAt(1).Label.ShouldEqual("FIELD2");
		                                     	};

		static string input;
		static IEnumerable<Field> fields;
	}

	public class When_parsing_a_field
	{
		Establish context = () =>
		                    input = "From \"SENDER\"";

		Because because = () =>
		                  field = EmailFormParser.Field.Parse(input);

		It should_have_a_Name = () =>
		                        field.Name.ShouldEqual("From");

		It should_have_a_Label = () =>
		                         field.Label.ShouldEqual("SENDER");

		static string input;
		static Field field;
	}

	public class When_parsing_an_identifier
	{
		Establish establish = () =>
		                      input = "From:";

		Because because = () =>
		                  id = EmailFormParser.Identifier.Parse(input);

		It should_be_named_From = () =>
		                          id.ShouldEqual("From");

		static string input;
		static string id;
	}

	public class When_parsing_a_label
	{
		Establish establish = () =>
		                      input = "\"SENDER\"";

		Because because = () =>
		                  label = EmailFormParser.Label.Parse(input);

		It should_be_labelled_SENDER = () =>
		                               label.ShouldEqual("SENDER");

		static string input;
		static string label;
	}
}